<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {ajax_link} function plugin
 *
 * Type:     function<br>
 * Name:     Include_js<br>
 * Purpose:  Generates Java Script for AJAX request
 * @param array
 * @param Smarty
 * @return string
 */
function smarty_function_ajax_submit($params, &$smarty)
{
	
    $url = '.';
	$update = null;
	$method = 'post';
	$asynchronous = 'true';
	$evalScripts = 'true';
	
	if ($smarty->ajax['loader_element']) {
		
		$onLoading = "Element.show('" . $smarty->ajax['loader_element'] . "')";
		$onComplete = "Element.hide('" . $smarty->ajax['loader_element'] . "')";
		
	}
	
	foreach ($params as $_key => $_value) {
		
		switch ($_key) {
			case 'url':
			case 'update':
			case 'method':
			case 'vars':
				$$_key = $_value;
				break;
			case 'success':
				$onSuccess = $_value;
				break;
		}
		
	}
	
	$_html_result = "new Ajax.Updater( {success :'$update'}, '$url', {asynchronous:	$asynchronous, method: '$method', evalScripts: $evalScripts ";
	
	if ($onComplete) $_html_result .=			", onComplete:		function(request) { $onComplete } ";
	if ($onLoading)  $_html_result .=			", onLoading:		function(request) { $onLoading } ";
	if ($onSuccess)  $_html_result .=			", onSuccess:		function(request) { $onSuccess } ";

	$_html_result .=							", parameters:		Form.serialize(this)";
	
	$_html_result .= 						"}); return false;";
	
    return $_html_result;

}


/* vim: set expandtab: */

?>
